package service

import (
	"context"
	"fmt"
	"log"
	"mts/trans_srv/internal/domain"
	"mts/trans_srv/internal/repository"
)

var (
	ErrRecordExist    = fmt.Errorf("记录已存在")
	ErrRecordNotFound = fmt.Errorf("记录不存在")
	ErrConflict       = fmt.Errorf("资源冲突")
)

type TranslationService interface {
	Insert(ctx context.Context, translation domain.Translation) error
	Update(ctx context.Context, translation domain.Translation) error
	Delete(ctx context.Context, translation domain.Translation) error
	GetTranslationsByLangId(ctx context.Context, id string) (*domain.ElemAndTransListWithLan, error)
	BatchTranslation(ctx context.Context, translations []domain.Translation) error
}
type TranslationServiceImpl struct {
	t repository.TranslationRepository
	l repository.LanguageRepository
	e repository.ElementRepository
}

func (t *TranslationServiceImpl) BatchTranslation(ctx context.Context, translations []domain.Translation) error {
	err := t.t.BatchTranslation(ctx, translations)
	if err != nil {
		return err
	}
	return nil
}

func (t *TranslationServiceImpl) GetTranslationsByLangId(ctx context.Context, id string) (*domain.ElemAndTransListWithLan, error) {
	// 1. 获取所有组件信息
	elements, err := t.e.FindAll(ctx)
	if err != nil {
		log.Printf("获取组件信息失败: %v", err)
		return nil, err
	}

	// 2. 构建元素 ID 到名称的映射
	elementMap := make(map[string]string)
	var ids []string
	for _, ele := range elements {
		elementMap[ele.ID] = ele.Name
		ids = append(ids, ele.ID)
	}

	// 3. 获取指定语言的翻译信息
	translations, err := t.t.GetAllElementsByLocale(ctx, id, ids...)
	if err != nil {
		log.Printf("获取翻译信息失败: %v", err)
		return nil, err
	}

	// 4. 初始化结果结构体
	var transistor domain.ElemAndTransListWithLan
	transistor.Lan = id
	transistor.Translations = make(map[string]string)

	// 5. 填充翻译信息
	for _, translation := range translations {
		if name, exists := elementMap[translation.ElementID]; exists {
			transistor.Translations[name] = translation.Content
		}
	}

	return &transistor, nil
}

func NewTranslationService(t repository.TranslationRepository, l repository.LanguageRepository, e repository.ElementRepository) TranslationService {
	return &TranslationServiceImpl{
		t: t,
		l: l,
		e: e,
	}
}

func (t *TranslationServiceImpl) Insert(ctx context.Context, translation domain.Translation) error {
	//1.先查找
	if err := t.t.IsExist(ctx, translation.ElementID, translation.Locale); err == nil {
		return ErrRecordExist
	}
	//2.插入数据
	if err := t.t.Insert(ctx, translation); err != nil {
		log.Printf("插入失败: %v", err)
		return err
	}
	return nil
}

func (t *TranslationServiceImpl) Update(ctx context.Context, translation domain.Translation) error {
	//1.先查找
	if err := t.t.IsExist(ctx, translation.ElementID, translation.Locale); err != nil {
		return err
	}
	//2.更新数据
	if err := t.t.Update(ctx, translation); err != nil {
		log.Printf("更新失败: %v", err)
		return err
	}
	return nil
}

func (t *TranslationServiceImpl) Delete(ctx context.Context, translation domain.Translation) error {
	//1.校验数据是否存在
	if err := t.t.IsExist(ctx, translation.ElementID, translation.Locale); err != nil {
		return err
	}
	if err := t.t.Delete(ctx, translation); err != nil {
		log.Printf("删除失败: %v", err)
		return err
	}
	return nil
}
